package com.lanxiaoye.shoppingSystem.service.impl;

import com.lanxiaoye.shoppingSystem.dao.UserMapper;
import com.lanxiaoye.shoppingSystem.entity.User;
import com.lanxiaoye.shoppingSystem.service.HomeLoginService;
import com.lanxiaoye.shoppingSystem.common.exception.BaseException;
import com.lanxiaoye.shoppingSystem.common.exception.EmptyException;
import com.lanxiaoye.shoppingSystem.dao.HomeLoginMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by libing on 2016/12/13.
 */
@Service
public class HomeLoginServiceImpl implements HomeLoginService {

    @Autowired
    HomeLoginMapper homeLoginMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public int checkExist(String name){
        int checkResult = 0;
        System.out.println("checkExist");
        checkResult = userMapper.checkByName(name) + userMapper.checkByPhone(name) + userMapper.checkByEmail(name);
        System.out.println(userMapper.checkByName(name));
        return checkResult;
    }

    @Override
    public User checkPwd(String name, String password){
        User user  = userMapper.selectByPassword(name,password);
        return user;
    }

    @Override
    public User selectPasswordAndKey(User user) throws BaseException,EmptyException  {
        try {
            if (user == null)
                throw new EmptyException("[HomeLoginServiceImpl][selectPasswordAndKey] phone empty");
            return homeLoginMapper.selectPasswordAndKey(user);
        }catch (EmptyException e){
            throw new EmptyException(e.getMessage());
        }catch (BaseException e){
            throw new BaseException("[HomeLoginServiceImpl][selectPasswordAndKey] exception");
        }
    }
}
